package com.fxsh.learn.shiro;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.springframework.util.DigestUtils;

/**
 * @Description 自定义用户校验规则
 * @Author Lrz
 * @date 2020/8/24 15:32
 */
public class MyCredentialsMatcher implements CredentialsMatcher {
    /**
     * @Description
     * @Param [authenticationToken:登录表单提交的用户验证信息, authenticationInfo：从doGetAuthenticationInfo方法获取大的realm中的用户验证信息]
     * @Return boolean
     * @Author Lrz
     * @date 2020/8/24 15:32
     */
    @Override
    public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String pwd = new String(token.getPassword());
        String md5Pwd = DigestUtils.md5DigestAsHex(pwd.getBytes());
        return authenticationInfo.getCredentials().equals(md5Pwd);
    }
}
